﻿using System.Collections.Generic;
using Crevice2D.WPFEditorHost.Infrastructure;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Patyk.MapEditor.Geometry;

namespace Patyk.MapEditor.Models
{
    public class Map
    {
        public string Name { get; set; }
        public float ScreenResolutionX { get; set; }
        public float ScreenResolutionY { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }

        public Vector3[] MapCorners { get; set; }

        public Texture2D Texture { get; set; }
        public Color[] Colors { get; set; }

        public List<InterpolatedCurve> Curves { get; set; }

        public Map(string name, int width, int height, float resolutionX, float resolutionY)
        {
            Name = name;
            Width = width;
            Height = height;
            ScreenResolutionX = resolutionX;
            ScreenResolutionY = resolutionY;

            MapCorners = new Vector3[4];
            MapCorners[0] = new Vector3(0, 0, 0);
            MapCorners[1] = new Vector3(width, 0, 0);
            MapCorners[2] = new Vector3(width, height, 0);
            MapCorners[3] = new Vector3(0, height, 0);

            Colors = new Color[width * height];
            for (int i = 0; i < width; ++i)
            {
                SetPixel(i, 0, Color.Red);
                SetPixel(i, height - 1, Color.Red);
            }
            for (int i = 0; i < height; ++i)
            {
                SetPixel(0, i, Color.Red);
                SetPixel(width - 1, i, Color.Red);
            }
            Texture = new Texture2D(RenderManager.Device, width, height, false, SurfaceFormat.Color);
            Texture.SetData(Colors);

            Curves = new List<InterpolatedCurve>();
        }

        public void SetPixel(int x, int y, Color color)
        {
            Colors[x + y * Width] = color;
        }

        public void Draw()
        {
            RenderManager.SpriteBatch.Draw(Texture, new Vector2(0, 0), Color.Red);
            //primitiveBatch.DrawClosedCurve(MapCorners.ToList(), Color.Red);
        }
    }
}
